<?php
include_once '/var/www/html/new/d/meituan/MT.class.php';
$meituan = new Meituan();

$txt=file_get_contents('php://input');
$meituan->logfile($txt,"MT_Notice_Status");

$ac='16ucom';
$pw='c33367701511b4f6020ec61ded352059';
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

$array = json_decode($txt);
$pft_order=  $array->Order16U;//票付通订单号
$ordercall =  $array->OrderCall;//远端订单号
$mt_orderids= explode("_",$ordercall );
$orderid = $mt_orderids[0]; //美团订单号
$actiontime = strtotime($array->ActionTime);//验证时间
$consume_num = $array->Tnumber; //消费数量
$app_key = 'ea59253a4292cbff10e3bb1ba1db01a490aabcf0'; //标示符
$posid =  'pft_001'; //pos机编号
$token = '40000032552645490'; 
$serial_num = '001'; //核销流水号
$v = '0.1';  //接口版本号
$signkey = '4866a0b329f143279a9ec80a8247b014818db49f';

$get_code =  $s->PFT_Order_Code("1853",$pft_order,1);
$get_code_xml=simplexml_load_string($get_code);
$verifycode= (string)($get_code_xml->Rec->UUcode);

$array_sign = array(
    'timestamp'=>$actiontime,
    'orderid'=>$orderid,
    'verifycode'=>$verifycode,
    'v'=>$v,
    'consume_num'=>$consume_num,
    'posid'=>$posid,
    'token'=>$token,
    'app_key'=>$app_key,
    'serial_num'=>$serial_num,   
);
$array_sign['sign']=_getSign($array_sign,$signkey); //计算sha1（）；
$post_data = http_build_query($array_sign);//连接数组
$url = 'http://e.meituan.com/api/eticketconsume';
$get_data= $url."?".$post_data;
// print_R($get_data);exit;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$get_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
$result = json_decode($output,true);
if($result['err_code'] == 0){
	echo 200;
}
function _getSign($array_sign,$signkey){
	ksort($array_sign);
	$s = $signkey;
	foreach ($array_sign as $k => $v) {
		if (!empty($v)) {
			$s .= $k . $v;
		}
	}
	return sha1($s);
}
// function logfile($txt,$path){
	// $fp = fopen($path,"a");
	// flock($fp, LOCK_EX) ;
	// fwrite($fp,$txt."\n");
	// flock($fp, LOCK_UN);
	// fclose($fp);
// }

// function logfile($txt,$path){
	// $fp = fopen('/var/www/html/new/d/logs/api/meituan/'.$path.date('Y-m-d').'.txt',"a");
	// flock($fp, LOCK_EX) ;
	// fwrite($fp,date('Y-m-d H:i:s').'|'.$txt."\n");
	// flock($fp, LOCK_UN);
	// fclose($fp);
// }

?>